<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:52
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Listeners\LocationLinkSourceListener;

/**
 * Class SeminarImageService
 * @package SDXApi\Service
 */
class SeminarImageService extends TemplateAbstract
{
	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|mixed
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$source = $this->sourceApp();
		$bind = $this->getUserPhoneBind($userId);
		$cacheName = 'seminar_template_page_image_' . $template['id'] . '_app_' . $source . '_bind_phone_' . $bind;
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->image($template);

			if (! empty($data)) {
				$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
			}
		}

		if (isset($data['link_data']) && empty($data['link_data'])) {
			$data['link_data'] = new \stdClass();
		}

		return $data;
	}

    protected function getUserPhoneBind($userId = 0)
    {
        $phone = D('Users')->findUserPhone($userId);

        if (empty($phone)) {
            return 0;
        }

        return 1;
    }

	/**
	 * Created by Jerry
	 * @param array $page
	 * @return array
	 */
	protected function image($page = array())
	{
//		旧版地址替换
		$page['image_url'] = self::checkImageUrlReplace($page['image_url']);

		$page['image_size_data'] = self::imageTypeSize($page);

//		$image_link = $this->imageLink($page['image_link']);
//		$page['link_data'] = $image_link;

		$locationLinkListener = new LocationLinkSourceListener();
		$linkData = $locationLinkListener->onCallLink($page['image_link']);
		if (! empty($linkData)) {
            $page['link_data'] = $linkData;
        }
//		$page['image_link'] = $locationLinkListener->onCallIosLink($page['image_link']);

		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['goods_type'], $page['goods_id'], $page['template_id']);

		return $page;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function imageLink($link)
	{
		$source = $this->sourceApp();

		$parameter = array();
		switch ($source) {
			case 1:
				$parameter = $this->androidUrlLink($link);
				break;
			case 2:
				$parameter = $this->iosUrlLink($link);
				break;
			case 3:
				$parameter = $this->h5UrlLink($link);
				break;
		}

		return $parameter;
	}

	/**
	 * 获取请求来源
	 * Created by Jerry
	 * @return int
	 */
	protected function sourceApp()
	{
		$is_app = I('post.is_app', 0, 'int');
		$is_ios = I('post.is_ios', 0, 'int');
		$is_h5 = I('post.is_h5', 0, 'int');

//		安卓
		if ($is_app && ($is_ios == 0) && ($is_h5 == 0)) {
			return 1;
		}

//		苹果
		if ($is_ios && ($is_app == 0) && ($is_h5 == 0)) {
			return 2;
		}

//		h5
		if ($is_h5 && ($is_ios == 0) && ($is_app == 0)) {
			return 3;
		}

		return 0;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array|object
	 */
	protected function androidUrlLink($link)
	{

		if (strpos($link, 'car')) {
			$data['link_new'] = $link;
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);
			$parameter = $this->convertUrlQuery($parseUrl['query']);
			$parameter = array_merge($parameter);
			$data['parameter'][] = $parameter;
			$data['page_name'] = 'cart';
			$data['is_new_link'] = 1;
		}

		if (empty($data)) {
			$data = new \stdClass();
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function iosUrlLink($link)
	{
		$data = array();

		if (strpos($link, 'car')) {
			$data['link_new'] = $link;
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);
			$parameter = $this->convertUrlQuery($parseUrl['query']);
			$data['parameter'][] = $parameter;
			$data['page_name'] = 'cart';
			$data['location_method'] = 1;
		}

		if (strpos($link, 'phone')) {
			$data['link_new'] = str_replace('phone', 'bindPhone', $link);
			$urlLink = 'https://w.shidexian.com' . $link;
			$parseUrl = parse_url($urlLink);
			$parameter = $this->convertUrlQuery($parseUrl['query']);
			$data['parameter'][] = $parameter;
			$data['page_name'] = 'bindPhone';
			$data['location_method'] = 1;
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $link
	 * @return array
	 */
	protected function h5UrlLink($link)
	{
		$data = array();

		if (strpos($link, 'car')) {
//			$data['link_new'] = $link;
//			$urlLink = 'https://w.shidexian.com' . $link;
//			$parameter = $this->convertUrlQuery($urlLink);
			$data['parameter'] = $link;
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $image
	 * @return mixed
	 */
	static protected function imageTypeSize($image)
	{
		$imageSize = array(
			1	=>	'750px 360px',
			2	=>	'750px 1100px',
			3	=>	'710px 360px',
			4	=>	'750px 750px',
			5	=>	'750px 176px',
			6	=>	'750px 360px',
			7	=>	'750px 250px',
			8	=>	'750px 900px',
			9	=>	'750px 100px',
			10	=>	'750px 550px',
			11	=>	'750px 50px',
            12  =>  '750px 700px',
            13  =>  '750px 300px',
            14  =>  '750px 220px',
            15  =>  '750px 300px',
            16  =>  '236px 236px',
            17  =>  '365px 260px',
            18  =>  '365px 440px',
            19  =>  '250px 340px',
		);

		return $imageSize[$image['image_size']];
	}

	/**
	 * Created by Jerry
	 * @param $query
	 * @return array
	 */
	static protected function convertUrlQuery($query)
	{
		$queryParts = explode('&', $query);
		$params = array();
		foreach ($queryParts as $param) {
			$item = explode('=', $param);
			$params[$item[0]] = $item[1];
		}
		return $params;
	}
}